package cn.mujiankeji.mbrowser.界面.标签页.搜索引擎

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.windowInsetsBottomHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import cn.mujiankeji.mbrowser.工具.clickable2
import cn.mujiankeji.mbrowser.数据.数据库.搜索引擎项目
import cn.mujiankeji.mbrowser.界面.图标.MyIconPack
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.保存
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.删除
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.向右
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.更多
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.菜单
import cn.mujiankeji.mbrowser.界面.标签页.统一标签页布局
import cn.mujiankeji.mbrowser.界面.组件.系统底部空间占位
import cn.mujiankeji.mbrowser.界面.组件.配置列表.配置行_输入文本
import 界面.弹窗.下拉菜单
import 界面.弹窗.下拉菜单分割线
import 界面.弹窗.下拉菜单项目


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun 搜索引擎编辑实现(state : 搜索引擎编辑状态) {
    state.coroutine = rememberCoroutineScope()
    DisposableEffect(Unit) {
        onDispose {
            state.coroutine = null
        }
    }

    统一标签页布局(title = {state.标题.value?:""},
        action = {
            var 展开更多菜单 by remember { mutableStateOf(false) }
            IconButton({
                state.保存()
            }) {
                Icon(MyIconPack.保存,"保存",
                    tint = MaterialTheme.colorScheme.surfaceTint)
            }
            IconButton({展开更多菜单 = !展开更多菜单}) {
                Box{
                    Icon(MyIconPack.更多,"更多",
                        tint = MaterialTheme.colorScheme.surfaceTint)
                    下拉菜单(展开更多菜单,{展开更多菜单 = false}){
                        下拉菜单项目("导出"){
                            state.导出()
                            展开更多菜单 = false
                        }
                        下拉菜单项目("导入"){
                            state.导入()
                            展开更多菜单 = false
                        }

                        下拉菜单分割线()
                        下拉菜单项目("删除"){
                            展开更多菜单 = false
                            state.删除()

                        }

                    }
                }
            }
        })
    {
        LazyColumn(Modifier.fillMaxSize()
            .background(MaterialTheme.colorScheme.background)){

            // 基本信息
            item {
                Column(Modifier.fillMaxWidth()
                    .background(MaterialTheme.colorScheme.surface)) {
                    配置行_输入文本("名称",state.当前编辑引擎.名称){
                        state.当前编辑引擎.名称 = it
                        true
                    }
                    配置行_输入文本("描述",state.当前编辑引擎.说明 ?:""){
                        state.当前编辑引擎.说明 = it
                        true
                    }
                }
            }

            item {
                Spacer(Modifier.height(10.dp)
                    .fillMaxWidth()
                    .background(MaterialTheme.colorScheme.background))
            }

            itemsIndexed(state.子引擎项目列表){index,item->
                if (item.展开){
                    子引擎展开(item, 删除 = {
                        state.删除子引擎(index)
                    })
                }
                else {
                    子引擎未展开(item)
                }


            }

            // 创建子引擎
            item {
                Text("添加子引擎", modifier = Modifier.padding(20.dp)
                    .clickable2 {
                        state.创建子引擎()
                    }.fillMaxWidth(),
                    textAlign = TextAlign.Center,
                    style = MaterialTheme.typography.labelMedium,
                    color = MaterialTheme.colorScheme.tertiary)
            }

            item {
                Spacer(Modifier.height(99.dp))
            }

        }
    }

}

@Composable
private fun 子引擎未展开(
    项目: 搜索引擎项目动态
) {

    Row(Modifier.fillMaxWidth()
        .padding(vertical = 3.dp)
        .clickable {
            项目.展开 = true
        }
        .background(MaterialTheme.colorScheme.surface)
        .defaultMinSize(minHeight = 65.dp)
        .padding(horizontal = 15.dp),
        verticalAlignment = Alignment.CenterVertically)
    {

        Column(Modifier.fillMaxWidth()){
            Text(项目.名称, style = MaterialTheme.typography.titleSmall,
                maxLines = 1)
            Spacer(Modifier.height(5.dp))
            Text(项目.地址, style = MaterialTheme.typography.bodyMedium,
                maxLines = 1)
        }

        Icon(MyIconPack.向右,"",
            modifier = Modifier.size(16.dp))

    }

}

@Composable
private fun 子引擎展开(
    项目 : 搜索引擎项目动态,
    删除 : ()->Unit
){

    Column(
        Modifier.fillMaxWidth()
            .padding(5.dp)
            .clip(MaterialTheme.shapes.extraSmall)
            .background(MaterialTheme.colorScheme.surface)
            .padding(horizontal = 15.dp)
    ) {

        // 头部  -- 删除 收起
        Row(Modifier.fillMaxWidth()
            .height(55.dp),
            verticalAlignment = Alignment.CenterVertically)
        {

            // 理论上这里可以切换类型 ---
            Spacer(Modifier.weight(1f))


            IconButton({删除()}) {
                Icon(MyIconPack.删除,"",
                    modifier = Modifier.size(16.dp),
                    tint = MaterialTheme.colorScheme.secondary)
            }

            IconButton({项目.展开 = false}) {
                Icon(MyIconPack.向右,"",
                    modifier = Modifier.size(18.dp),
                    tint = MaterialTheme.colorScheme.secondary)
            }

        }

        // 编辑位
        OutlinedTextField(
            value = 项目.名称,
            onValueChange = { 项目.名称 = it },
            label = { Text("名称") },
            modifier = Modifier.fillMaxWidth(),
            singleLine = true
        )

        OutlinedTextField(
            value = 项目.地址,
            onValueChange = { 项目.地址 = it },
            label = { Text("地址") },
            modifier = Modifier.fillMaxWidth(),
            singleLine = true
        )

        Spacer(Modifier.height(5.dp))


        OutlinedTextField(
            value = 项目.POST,
            onValueChange = { 项目.POST = it },
            label = { Text("POST(可空)") },
            modifier = Modifier.fillMaxWidth(),
            singleLine = true
        )

        Spacer(Modifier.height(5.dp))

        OutlinedTextField(
            value = 项目.编码,
            onValueChange = { 项目.编码 = it },
            label = { Text("编码(默认UTF8)") },
            modifier = Modifier.fillMaxWidth(),
            singleLine = true
        )



    }



}


